package com.attack.web;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.attack.manager.CustomerInfoManager;
import com.attack.manager.InterestInfoManager;
import com.attack.model.CustomerInfoModel;
import com.common.component.Pagination;
import com.common.manager.CparamManager;
import com.common.manager.StaffManager;
import com.common.model.StaffInfoModel;
import com.framework.model.IData;
import com.framework.model.impl.DataMap;
import com.framework.web.BaseController;

@Controller
@RequestMapping(value = "/management/customer")
public class CustomerInfoController extends BaseController{

	 private static String modelUrl="/management/customer/";
	 
    @Autowired
	private CustomerInfoManager customerInfoManager;
    
    @Autowired
	private InterestInfoManager interestInfoManager;
    
    @Autowired
	private StaffManager staffManager;
    
    @Autowired
	private CparamManager cparamManager;
    
    /**
     * 功能：页面分页列表
     * @param vo
     * @param param
     * @return
     */
	@RequestMapping("/pageList")
	public ModelAndView pageList(@ModelAttribute("page") Pagination vo,@ModelAttribute("customerInfoModel") CustomerInfoModel param) {
		ModelAndView modelView = new ModelAndView(modelUrl+"list");
		List<DataMap> dataList = customerInfoManager.getPageObjectList(vo, param);
		modelView.addObject("vo", vo);
		modelView.addObject("param", param);
		modelView.addObject("objList", dataList);
		return modelView;
	}
	
	
	/**
	 * 功能：初始化添加页面
	 * @param customerInfoModel
	 * @return
	 */
	@RequestMapping("initAdd")
	public ModelAndView initAdd(@ModelAttribute("customerInfoModel") CustomerInfoModel customerInfoModel) {
		ModelAndView modelView = new ModelAndView(modelUrl+"add");
		log.info("add is page");
		cparamManager.getCparamList(modelView, "'SYS_CUSTLEVEL','SYS_PSEX','SYS_BANKNAME'");
		
		IData staffMap = new DataMap();
		staffMap.put("orgCode", customerInfoModel.getOrgCode());
		List staffList = staffManager.getObjectList(staffMap);
		modelView.addObject("staffList",staffList);
		modelView.addObject("vo",customerInfoModel);
		return modelView;
	}
	
	/**
	 * 功能：初始化编辑页面
	 * @param intID
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/{intID}/initEdit")
	public ModelAndView initEdit(@PathVariable("intID") String intID) {
		ModelAndView modelView = new ModelAndView(modelUrl+"update");
		IData dataMap = new DataMap();
		dataMap.put("intID", intID);
		cparamManager.getCparamList(modelView, "'SYS_CUSTLEVEL','SYS_PSEX','SYS_BANKNAME'");
		
		CustomerInfoModel customerInfoModel = (CustomerInfoModel)customerInfoManager.getObjectInfo(dataMap);
		
		IData staffMap = new DataMap();
		staffMap.put("orgCode", customerInfoModel.getOrgCode());
		List staffList = staffManager.getObjectList(staffMap);
		modelView.addObject("staffList",staffList);
		
		modelView.addObject("vo",customerInfoModel);
		return modelView;
	}
	
	/**
	 * 功能：执行插入操作
	 * @param customerInfoModel
	 * @return
	 */
	@ResponseBody
	@RequestMapping("add")
	public Object insert(HttpServletRequest request,HttpServletResponse response,@ModelAttribute("customerInfoModel") CustomerInfoModel customerInfoModel) {
		try{
			int opNum = customerInfoManager.insertObjectAndGetID(customerInfoModel);
			if(opNum>0){
				return super.jsonReturn(response,200,true);
			}else{
				return super.jsonReturn(response,300,true);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return super.jsonReturn(response,300,e.getMessage(),true);
		}
	}
	
	/**
	 * 功能：执行更新操作
	 * @param customerInfoModel
	 * @return
	 */
	@ResponseBody
	@RequestMapping("update")
	public Object update(HttpServletRequest request,HttpServletResponse response,@ModelAttribute("customerInfoModel") CustomerInfoModel customerInfoModel) {
		try{
			int opNum =customerInfoManager.updateObject(customerInfoModel);
			if(opNum>0){
				return super.jsonReturn(response,200,true);
			}else{
				return super.jsonReturn(response,300,true);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return super.jsonReturn(response,300,e.getMessage(),true);
		}
	}
	
	/**
	 * 功能：执行单记录删除
	 * @param ids
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/{intID}/delete")
	public Object delete(HttpServletResponse response,@PathVariable("intID") int intID) {
		try{
			IData dataMap = new DataMap();
			dataMap.put("intID", intID);
			int opNum =customerInfoManager.deleteObject(dataMap);
			if(opNum>0){
				interestInfoManager.deleteCustInterest(String.valueOf(intID));
				return super.jsonReturn(response,200,false);
			}else{
				return super.jsonReturn(response,300,false);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return super.jsonReturn(response,300,e.getMessage(),false);
		}
	}
	
	/**
	 * 功能：执行批量删除
	 * @param ids
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/deleteAll")
	public Object deleteAll(HttpServletResponse response,HttpServletRequest request) {
		String ids = request.getParameter("ids");
		try{
			IData dataMap = new DataMap();
			dataMap.put("ids", ids);
			int opNum =customerInfoManager.deleteObject("deleteAllObject",dataMap);
			if(opNum>0){
				interestInfoManager.deleteCustInterest(ids);
				return super.jsonReturn(response,200,false);
			}else{
				return super.jsonReturn(response,300,false);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return super.jsonReturn(response,300,e.getMessage(),false);
		}
	}
	

	
	/**
	 * 功能：机构人员树
	 * @param response
	 * @param request
	 */
	@ResponseBody
	@RequestMapping("/loadOrgStaffTreeList")
	public void loadOrgStaffTreeList(HttpServletResponse response,HttpServletRequest request){
		StaffInfoModel staffInfoModel = super.getUserSession(request);
		String roleId = staffInfoModel.getRoleids();
		String menuFunc = customerInfoManager.loadOrgStaffTreeList(roleId);
		this.ajaxJson(response, menuFunc);
	}
	/**
	 * 地图
	 * @param vo
	 * @param param
	 * @return
	 */
	@RequestMapping("/showMap")
	public ModelAndView showMap(@RequestParam("mapParam") String mapParam,HttpServletRequest request) {
		ModelAndView modelView = new ModelAndView(modelUrl+"map");
		try {
			mapParam = new String(request.getParameter("mapParam").getBytes("iso-8859-1"),"utf-8") ;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		modelView.addObject("mapParam", mapParam);
		System.out.println(mapParam);
		return modelView;
	}
}